package com.xja.zf.controller;

import com.xja.zf.entity.Users;
import com.xja.zf.service.UsersService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("users")
public class UsersController {
    private Logger logger= LoggerFactory.getLogger(UsersController.class);
    @Reference(url = "dubbo://localhost:20881")
    private UsersService usersService;

    @RequestMapping("login")
    public String login(Users users, HttpSession session){
        logger.info("这是登录的方法");
        try {
            Users users1=usersService.login(users);
            if(users1!=null){
                session.setAttribute("users",users1);
                return "redirect:/manage/init";
            }
        }catch (Exception ex){
            logger.error("登录失败",ex);
        }
        return "login";

    }

    @RequestMapping("regist")
    public String regist(Model model, Users users){
        logger.info("注册的控制器-->"+users.getName());
        Users userExist = usersService.selectUserExist(users.getName());
        if(userExist!=null){
            //用户名已存在
            return "regs";
        }
        //不存在则注册用户
        int insertCount = usersService.insertSelective(users);
        if(insertCount==1){
            System.out.println("注册成功");
            model.addAttribute("msg","注册成功");
        }else {
            System.out.println("注册失败");
            model.addAttribute("msg","注册失败");
        }
        return "login";

    }
}
